#include "login.h"
#include "ui_login.h"

login::login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::login)
{
    ui->setupUi(this);
    initfaultcondition();
    connectsignals();
    this->setWindowFlags(Qt::FramelessWindowHint);
    timer_enroll->start(100);
    timer_login->start(100);
    ui->stackedWidget->setCurrentIndex(1);
    ui->convertloginbtn->setStyleSheet("color:#4393d5;border:none;");
}

login::~login()
{
    delete ui;
}

void login:: initfaultcondition()
{
    loginfault=new QLabel(this);
    enrollfault=new QLabel(this);
    loginfault->setGeometry(300,900,150,35); //900->700
    enrollfault->setGeometry(300,900,150,35);
    loginfault->setStyleSheet("background-color:#5b5b5b;color:#e5e5e5;font-size:10;border-radius:10px;");
    enrollfault->setStyleSheet("background-color:#5b5b5b;color:#e5e5e5;font-size:10;border-radius:10px;");
    loginfault->setText("用户名或密码错误");
    enrollfault->setText("注册失败");
    loginfault_an=new QPropertyAnimation(loginfault,"pos",this);
    enrollfault_an=new QPropertyAnimation(enrollfault,"pos",this);
    loginfault_an->setDuration(300);
    enrollfault_an->setDuration(300);
    loginfault_an->setStartValue(QPoint(500,300));
    enrollfault_an->setStartValue(QPoint(500,300));
    loginfault_an->setEndValue(QPoint(500,165));
    enrollfault_an->setEndValue(QPoint(500,165));
    connect(enrollfault_an,&QPropertyAnimation::finished,this,[=](){
        startcount_enroll=true;
    });
    connect(loginfault_an,&QPropertyAnimation::finished,this,[=](){
        startcount_login=true;
    });
    loginfault->close();
    enrollfault->close();
}

void login::connectsignals()
{
    connect(timer_enroll,&QTimer::timeout,this,[=](){
        if(ableenroll)
        {
            close();
        }
        else
        {
            if(enroll_result)
            {
                enrollfault->show();
                enrollfault_an->start();
                enroll_result=false;
            }
            if(startcount_enroll)
            {
                anima_time+=70;
            }
            if(anima_time>=anima_duration)
            {
                enrollfault->close();
                startcount_enroll=false;
                anima_time=0;
            }

        }
    });
    connect(timer_login,&QTimer::timeout,this,[=](){
        if(ablelogin)
        {
            close();

        }
        else
        {
            if(login_result)
            {
                loginfault->show();
                loginfault_an->start();
                login_result=false;
            }
            if(startcount_login)
            {
                anima_time+=70;
            }
            if(anima_time>=anima_duration)
            {
                anima_time=0;
                loginfault->close();
                startcount_login=false;
            }

        }
    });
}



void login::on_enrollbtn_clicked()
{
    bool c1=ui->ea->text().isEmpty();
    bool c2=ui->en->text().isEmpty();
    bool c3=ui->es->text().isEmpty();
    QString s1=ui->ea->text();
    QString s2=ui->en->text();
    QString s3=ui->es->text();
    if(!c1&&!c2&&!c3)
    {
        emit enrollinfo(s1,s2,s3,default_image);
    }
}


void login::on_loginbtn_clicked()
{
    bool c1=ui->la->text().isEmpty();
    bool c2=ui->ls->text().isEmpty();
    QString s1=ui->la->text();
    QString s2=ui->ls->text();
    if(!c1&&!c2)
    {
        emit addinfo(s1,s2);
    }
}



void login::on_convertenrollbtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->convertenrollbtn->setStyleSheet("color:#4393d5;border:none;");
    ui->convertloginbtn->setStyleSheet("color:black;border:none;");
}


void login::on_convertloginbtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    ui->convertloginbtn->setStyleSheet("color:#4393d5;border:none;");
    ui->convertenrollbtn->setStyleSheet("color:black;border:none;");
}


void login::on_pushButton_2_clicked()
{
    close();
}

